package cn.coopen.italktv.jnidemo_cmaker2;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.File;

public class MainActivity extends AppCompatActivity {
	private static final String TAG ="MainActivity";
	private String SD_CARD_PATH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SD_CARD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
		/**
		 * 1. 检查权限
		 * 2. 申请权限
		 * 3. 处理申请回掉
		 * */
		if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!=
				PackageManager.PERMISSION_GRANTED){
			ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
			Manifest.permission.WRITE_EXTERNAL_STORAGE},200);
		}
	}

	/**
	 * 拆分
	 * @param btn
	 */
	public void mDiff(View btn){
		String path = SD_CARD_PATH + File.separatorChar+ "demo.mp3";
		String path_pattern = SD_CARD_PATH +File.separatorChar+ "demo_%d.mp3";

		System.out.println(SD_CARD_PATH);
		NDKFileUtils.diff(path, path_pattern, 3);
		Log.d("jason", "拆分完毕..");
	}

	/**
	 * 合并
	 * @param btn
	 */
	public void mPatch(View btn){
		String path_pattern = SD_CARD_PATH +File.separatorChar+ "demo_%d.mp3";
		String merge_path = SD_CARD_PATH +File.separatorChar+ "demo_merge.mp3";
		NDKFileUtils.patch(path_pattern, 3, merge_path);
		Log.d("jason", "合并完毕..");

	}


	@Override
	public void onRequestPermissionsResult(int requestCode,
	                                       @NonNull String[] permissions,
	                                       @NonNull int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if (requestCode == 200){

			if (grantResults.length==2){
				System.out.println("两个权限都通过了");
			}

		}
	}
}
